package main

import (
	"fmt"
	"time"

	"gitee.com/yctxkj/eventbus/eventbus"
)

func main() {
	fmt.Println("hello")

	// 初始化，消息总线
	bus := eventbus.NewEventBus()

	// 订阅消息
	bus.Subscribe("msg", func(e eventbus.Event) {
		fmt.Println("--> recv", e)
	})

	// 发布消息
	bus.Publish("msg", eventbus.Message{
		UUID:    eventbus.NewShortUUID(),
		Payload: []byte("hello world!"),
	})
	time.Sleep(1 * time.Second)

	// 注册请求处理器
	bus.RegisteHandler("handler", func(event eventbus.Event) (response *eventbus.Message, err error) {
		fmt.Println(event)
		time.Sleep(1 * time.Second)
		fmt.Println("2", event)
		return
	})

	// 发送请求到总线
	bus.RequestHandler("handler", eventbus.Message{
		UUID:    eventbus.NewUUID(),
		Payload: []byte("请求"),
	})

}
